﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace XFPPT.NET.Models
{
    /// <summary>
    /// 响应描述类，包含整个响应的相关信息
    /// Response description class, containing relevant information of the entire response
    /// </summary>
    public class ProgressResponse
    {
        /// <summary>
        /// 响应标识
        /// Response flag
        /// </summary>
        public bool? flag { get; set; }

        /// <summary>
        /// 错误码
        /// Error code
        /// </summary>
        public long? code { get; set; }

        /// <summary>
        /// 错误详情
        /// Error details
        /// </summary>
        public string? desc { get; set; }

        /// <summary>
        /// 不用关注，预留
        /// Don't pay attention, reserved
        /// </summary>
        public long? count { get; set; }

        /// <summary>
        /// 包含PPT构建、AI配图、演讲备注等详细状态和信息的数据对象
        /// Data object containing detailed status and information such as PPT construction, AI image matching, and speech notes
        /// </summary>
        public ProgressResponseData? data { get; set; }

        /// <summary>
        /// 会话ID，暂未使用
        /// Session ID, not currently used
        /// </summary>
        public object? sid { get; set; }

        [JsonIgnore]
        private string realJsonstring = string.Empty;

        /// <summary>
        /// Gets or sets the real JSON string representation of the response.
        /// 获取或设置响应的真实 JSON 字符串表示。
        /// </summary>
        [JsonIgnore]
        public string? RealJsonstring
        {
            get { return realJsonstring; }
            set
            {
                realJsonstring = value!;
                try
                {
                    DeserializeProgressResponse(value!);
                }
                catch (Exception)
                {
                    // 可添加日志记录等操作
                }
            }
        }

        /// <summary>
        /// 反序列化进度响应的 JSON 字符串到当前对象。
        /// 此方法将传入的 JSON 字符串解析为对应的属性值，并赋值给当前对象的各个属性。
        /// </summary>
        /// <param name="json">待反序列化的 JSON 字符串。</param>
        public virtual void DeserializeProgressResponse(string json)
        {
            try
            {
                JsonNode? jsonNode = JsonNode.Parse(json);
                if (jsonNode != null)
                {
                    try
                    {
                        if (jsonNode["flag"] != null)
                        {
                            this.flag = Convert.ToBoolean(jsonNode["flag"]!.ToString());
                        }
                    }
                    catch { }
                    try
                    {
                        if (jsonNode["code"] != null)
                        {
                            this.code = Convert.ToInt64(jsonNode["code"]!.ToString());
                        }
                    }
                    catch { }
                    try
                    {
                        if (jsonNode["desc"] != null)
                        {
                            this.desc = jsonNode["desc"]!.ToString();
                        }
                    }
                    catch { }
                    try
                    {
                        if (jsonNode["count"] != null)
                        {
                            this.count = Convert.ToInt64(jsonNode["count"]!.ToString());
                        }
                    }
                    catch { }
                    if (jsonNode["data"] != null)
                    {
                        this.data = new ProgressResponseData();
                        var dataNode = jsonNode["data"]!;
                        try
                        {
                            if (dataNode["pptStatus"] != null)
                            {
                                this.data.pptStatus = dataNode["pptStatus"]!.ToString();
                            }
                        }
                        catch { }
                        try
                        {
                            if (dataNode["aiImageStatus"] != null)
                            {
                                this.data.aiImageStatus = dataNode["aiImageStatus"]!.ToString();
                            }
                        }
                        catch { }
                        try
                        {
                            if (dataNode["cardNoteStatus"] != null)
                            {
                                this.data.cardNoteStatus = dataNode["cardNoteStatus"]!.ToString();
                            }
                        }
                        catch { }
                        try
                        {
                            if (dataNode["pptUrl"] != null)
                            {
                                this.data.pptUrl = dataNode["pptUrl"]!.ToString();
                            }
                        }
                        catch { }
                        try
                        {
                            if (dataNode["errMsg"] != null)
                            {
                                this.data.errMsg = dataNode["errMsg"]!.ToString();
                            }
                        }
                        catch { }
                        try
                        {
                            if (dataNode["totalPages"] != null)
                            {
                                this.data.totalPages = Convert.ToInt64(dataNode["totalPages"]!.ToString());
                            }
                        }
                        catch { }
                        try
                        {
                            if (dataNode["donePages"] != null)
                            {
                                this.data.donePages = Convert.ToInt64(dataNode["donePages"]!.ToString());
                            }
                        }
                        catch { }
                    }
                    try
                    {
                        if (jsonNode["sid"] != null)
                        {
                            this.sid = jsonNode["sid"]!.ToString();
                        }
                    }
                    catch { }
                }
            }
            catch { }
        }
    }

    /// <summary>
    /// 包含PPT构建、AI配图、演讲备注等详细状态和信息的数据类
    /// Data class containing detailed status and information such as PPT construction, AI image matching, and speech notes
    /// </summary>
    public class ProgressResponseData
    {
        /// <summary>
        /// PPT构建状态：building（构建中），done（已完成），build_failed（生成失败）
        /// PPT construction status: building (in progress), done (completed), build_failed (build failed)
        /// </summary>
        public string? pptStatus { get; set; }

        /// <summary>
        /// ai配图状态：building（构建中），done（已完成）
        /// AI image matching status: building (in progress), done (completed)
        /// </summary>
        public string? aiImageStatus { get; set; }

        /// <summary>
        /// 演讲备注状态：building（构建中），done（已完成）
        /// Speech note status: building (in progress), done (completed)
        /// </summary>
        public string? cardNoteStatus { get; set; }

        /// <summary>
        /// 生成PPT的地址
        /// Address of the generated PPT
        /// </summary>
        public string? pptUrl { get; set; }

        /// <summary>
        /// 生成PPT的失败信息
        /// Failure information of PPT generation
        /// </summary>
        public string? errMsg { get; set; }

        /// <summary>
        /// 生成PPT的总页数
        /// Total number of pages of the generated PPT
        /// </summary>
        public long? totalPages { get; set; }

        /// <summary>
        /// 生成PPT的完成页数 （ai配图和演讲备注为异步任务，ppt页数完成，不代表配图和演讲备注也完成）
        /// Number of completed pages of the generated PPT (AI image matching and speech notes are asynchronous tasks. Completion of PPT pages does not mean that image matching and speech notes are also completed)
        /// </summary>
        public long? donePages { get; set; }
    }
}
